

function FancyTreeHelper ($rootScope, $scope, dataProvider) {
    this.dataProvider = dataProvider;

    FancyTreeHelper.prototype.BuildTree = function()
    {
        $("#tree").fancytree({
            extensions:["filter"],
            source:dataProvider,
            icons: false,

            filter:{
                mode:"hide"
            },
            activate:function (e, data) {
                var node = data.tree.getFirstChild();
                node.setSelected(false);

                var scope = angular.element('[ng-controller=TreeCtrl]').scope();
                scope.$apply(function () {
                    scope.selectedNode = data;

                    $rootScope.$broadcast("TREE_NODE_SELECTED_EVENT", {orgKey:data.node.key, isLeaf:!data.node.folder, orgText:data.node.title });
                });
            },

            expand: function(event, data) {
                $('#splitter-bar').each(function(){
                    $(this).css('height', $("#left_area").height()-62);
                })

                $('.ui-fancytree').each(function(){
                    $(this).css('max-height', $(window).height()-210);
                });
            },

            collapse: function(event, data) {
                $('#splitter-bar').each(function(){
                    $(this).css('height', $("#left_area").height()-62);
                })
            },

            init:function (event, data) {
                //Expand the root node
                if(!$scope.treeHasBeenLoaded)
                {
                    var node = data.tree.getFirstChild();
                    if(node !== 'undefined')
                    {
                        node.setSelected(true);
                        node.setFocus(true);
                        node.setExpanded();
                        $rootScope.$broadcast("TREE_NODE_SELECTED_EVENT", {orgKey:node.key, isLeaf:!node.folder, orgText:node.title });
                    }
                }

                $('#splitter-bar').each(function(){
                    $(this).css('height', $("#left_area").height()-62);
                })
            }

        });
        var tree = $("#tree").fancytree("getTree");
        $("input[name=search]").keyup(
            function (e) {
                var match = $(this).val();
                if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(match) === "") {
                    $("button#btnResetSearch").click();
                    return;
                }
                // Pass text as filter string (will be matched as substring in the node title)
                var n = tree.applyFilter(match);
                $("button#btnResetSearch").attr("disabled", false);
                $("span#matches").text("(" + n + " matches)");

                $('#splitter-bar').each(function(){
                    $(this).css('height', $("#left_area").height()-62);
                })

            }).focus();
        $("button#btnResetSearch").click(
            function (e) {
                $("input[name=search]").val("");
                $("span#matches").text("");
                tree.clearFilter();
            }).attr("disabled", true);
        $("input#hideMode").change(function (e) {
            tree.options.filter.mode = $(this).is(":checked") ? "hide" : "dimm";
            tree.clearFilter();
            $("input[name=search]").keyup();
        });
    }
}